﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IWshRuntimeLibrary;
using File = System.IO.File;

namespace LaunchMeNot.Core
{
    /// <summary>
    /// A shortcut file (.lnk)
    /// </summary>
    public class ShellShortcut : StartupEntryBase
    {
        protected string _shortcutFilename,
                         _description;
        protected string _originalName;

        /// <summary>
        /// Gets the original name of the shortcut file (used as a filename).
        /// </summary>
        /// <value>The original name of the shortcut file (used as a filename).</value>
        public string OriginalName
        {
            get { return _originalName; }
            private set
            {
                _originalName = value;
                OnPropertyChanged("OriginalName");
            }
        }

        /// <summary>
        /// Gets or sets the filename of the .lnk file.
        /// </summary>
        /// <value>The filename of the .lnk file.</value>
        public string ShortcutFilename
        {
            get { return this.Name.EndsWith(".lnk", StringComparison.InvariantCultureIgnoreCase) ? this.Name : this.Name + ".lnk"; }
        }

        /// <summary>
        /// Gets or sets the description of the shortcut.
        /// </summary>
        /// <value>The description of the shortcut.</value>
        public string Description
        {
            get { return _description; }
            set
            {
                _description = value;
                OnPropertyChanged("Description");
            }
        }

        public ShellShortcut() 
            :base(){
                Description = string.Empty;
        }

        /// <summary>
        /// Initializes a <see cref="ShellShortcut"/> from an existing file. 
        /// If the file is a shortcut (.lnk) file, it's properties are used. Otherwise, the files name, path and arguments are parsed.
        /// </summary>
        /// <param name="shortcutPath">The shortcut path.</param>
        public ShellShortcut(string shortcutPath) 
                :base() {
            if (!File.Exists(shortcutPath))
                throw new ArgumentException(string.Format("Shortcut '{0}' does not exist", shortcutPath ?? string.Empty));
            if (System.IO.Path.GetExtension(shortcutPath).Equals(".lnk", StringComparison.InvariantCultureIgnoreCase)) {
                var shell = new WshShell();
                IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);
                this.Name = System.IO.Path.GetFileNameWithoutExtension(shortcutPath);
                this.Description = shortcut.Description;
                this.Path = shortcut.TargetPath;
                this.Arguments = shortcut.Arguments;
                _originalName = this.ShortcutFilename;
                //this.ShortcutFilename = shortcutPath;
            } else {
                this.Name = System.IO.Path.GetFileNameWithoutExtension(shortcutPath);
                var pair = Utils.SplitArguments(shortcutPath);
                this.Path = pair.Key;
                this.Arguments = pair.Value;
                //this.ShortcutFilename = this.Name + ".lnk";
            }
        }

        /// <summary>
        /// Initializes a new <see cref="ShellShortcut"/>.
        /// </summary>
        /// <param name="name">The friendly name.</param>
        /// <param name="path">The path of the file to launch.</param>
        /// <param name="arguments">The arguments.</param>
        public ShellShortcut(string name, string path, string arguments)
            : this() {
            this.Name = name;
            this.Path = path;
            this.Arguments = arguments;
            _originalName = this.ShortcutFilename;
            //this.ShortcutFilename = shortcutFilename;
        }

        /// <summary>
        /// Initializes a new <see cref="ShellShortcut"/>.
        /// </summary>
        /// <param name="entry">The entry.</param>
        public ShellShortcut(IStartupEntry entry)
            : this() {
            this.Name = entry.Name;
            this.Path = entry.Path;
            this.Arguments = entry.Arguments;
            //this.ShortcutFilename = System.IO.Path.GetFileName(this.Name) + ".lnk";
            if (entry is LaunchMeNotEntry) {
                this.Description = ((LaunchMeNotEntry) entry).Description;
            } else if (entry is ShellShortcut) {
                this.Description = ((ShellShortcut)entry).Description;
            }
        }

        /// <summary>
        /// Clones this instance.
        /// </summary>
        /// <returns>A deep copy of this entry.</returns>
        public override IStartupEntry Clone()
        {
            var entry = new ShellShortcut()
            {
                Name = this.Name,
                Path = this.Path,
                Arguments = this.Arguments,
                OriginalName = this.OriginalName,
                //ShortcutFilename = this.ShortcutFilename,
                Description = this.Description,
                EntryType = this.EntryType
            };
            return entry;
        }


        public override string ToString()
        {
            return string.Format("{0} - {1}", Name ?? string.Empty, Path ?? string.Empty);
        }
    }
}
